package cmich.cps.svn.utility.gui;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

import cmich.cps.svn.utility.Settings;

/**
 * The Class LoginDialog.
 *
 * @author adapted from the web
 */
public class LoginDialog extends JDialog {
	
	/** The Constant serialVersionUID. */
	private static final long serialVersionUID = 1L;
	
	/** The username field. */
	private JTextField usernameField;
    
    /** The password field. */
    private JPasswordField passwordField;
    
    /** The username label. */
    private JLabel lblUsername;
    
    /** The password label. */
    private JLabel lblPassword;
    
    /** The btn login. */
    private JButton btnLogin;
    
    /** The btn cancel. */
    private JButton btnCancel;
    
    /** The auth checkbox. */
    private JCheckBox authCheckbox;
    
    /**
     * Instantiates a new login dialog.
     *
     * @param parent the parent
     */
    public LoginDialog(Frame parent) {
        super(parent, "Login", true);
        JPanel panel = new JPanel(new GridBagLayout());
        GridBagConstraints cs = new GridBagConstraints();

        cs.fill = GridBagConstraints.HORIZONTAL;
        cs.insets = new Insets(5, 5, 0, 0);
        cs.ipadx = 5;
        cs.ipady = 5;

        lblUsername = new JLabel("Username: ");
        cs.gridx = 0;
        cs.gridy = 0;
        cs.gridwidth = 1;
        panel.add(lblUsername, cs);

        usernameField = new JTextField(20);
        cs.gridx = 1;
        cs.gridy = 0;
        cs.gridwidth = 2;
        panel.add(usernameField, cs);

        lblPassword = new JLabel("Password: ");
        cs.gridx = 0;
        cs.gridy = 1;
        cs.gridwidth = 1;
        panel.add(lblPassword, cs);

        passwordField = new JPasswordField(20);
        cs.gridx = 1;
        cs.gridy = 1;
        cs.gridwidth = 2;
        panel.add(passwordField, cs);
        
        authCheckbox = new JCheckBox("Save authentication");
        cs.gridx = 0;
        cs.gridy = 2;
        cs.gridwidth = 2;
        panel.add(authCheckbox, cs);
        
        btnLogin = new JButton("OK");
        btnLogin.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                Settings.getInstance().setUsername(getUsername());
                Settings.getInstance().setPassword(getPassword());
                Settings.getInstance().setAuthCache(getAuthCache());
                dispose();
            }
        });
        btnCancel = new JButton("Cancel");
        btnCancel.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                dispose();
            }
        });
        JPanel bp = new JPanel();
        bp.add(btnLogin);
        bp.add(btnCancel);

        getContentPane().add(panel, BorderLayout.CENTER);
        getContentPane().add(bp, BorderLayout.PAGE_END);
        getRootPane().setDefaultButton(btnLogin);

        pack();
        setResizable(false);
        setLocationRelativeTo(parent);
    }

    /**
     * Gets the username.
     *
     * @return the username
     */
    public String getUsername() {
        return usernameField.getText().trim();
    }

    /**
     * Gets the password.
     *
     * @return the password
     */
    public String getPassword() {
        return new String(passwordField.getPassword());
    }
    
    /**
     * Gets the auth cache.
     *
     * @return the auth cache
     */
    public boolean getAuthCache() {
        return authCheckbox.isSelected();
    }
}
